package model

import (
	"database/sql"
	"gorm.io/gorm"
)

type OrderGoods struct {
	gorm.Model
	SonOrderNum     string       `gorm:"type:varchar(18);not null"`
	OrderId         int32        `gorm:"type:int(11);not null"`
	GoodsSkuId      int32        `gorm:"type:int(11);not null"`
	Number          int32        `gorm:"type:tinyint(3);not null"`
	SumPrice        string       `gorm:"type:decimal(10,2);not null"`
	Status          int32        `gorm:"type:tinyint(1);not null;default:1;comment:'订单状态 0支付完成 1待支付 2取消支付 3退款中 4退款完成'"` //订单状态 0支付完成 1待支付 2取消支付 3退款中 4退款完成
	LogisticsStatus int32        `gorm:"type:tinyint(1);default:0;comment:'物流状态 0等待付款中 1待发货 2待收货 3已收货 4退货中 5退货完成'"`     //物流状态 1待发货 2待收货 3已收货 4退货中 5退货完成
	RefundTime      sql.NullTime //退款时间
}

func (OrderGoods) TableName() string {
	return "order_goods"
}
func FindOrderGoodsById(id int32) (order *OrderGoods, err error) {
	var orderGoods OrderGoods
	result := Db.Model(&OrderGoods{}).Where("id = ?", id).First(&orderGoods)
	if result.Error != nil {
		return nil, err
	}
	return &orderGoods, nil
}
func DeleteOrderGoodsByOrderId(id int32) error {
	err := Db.Where("order_id = ?", id).Delete(&OrderGoods{}).Error
	return err
}

func FindOrderGoodsIsPayById(id int32) bool {
	var orderGoods OrderGoods
	result := Db.Model(&OrderGoods{}).Where("id = ?", id).First(&orderGoods)
	if result.Error != nil && result.RowsAffected == 0 || orderGoods.Status != 0 {
		return false
	}
	return true
}
